<?php 
session_start();

if ($_SESSION['memberID']) { $page_title = "Edit Album"; } else { $page_title = "View Album"; }

$filter = $_GET['filter'];
 $link = mysql_connect('localhost', 'kootenay', 'nonsense');
 if (!$link) {
  die ('Not connected : ' . mysql_error());
 }
 $db_selected = mysql_select_db('kootenay', $link);

 if (!$db_selected) {
  die ('Can\'t use foo : ' . mysql_error());
 }

$mode = $_GET['mode'];
$aid = $_GET['aid']; if (!$aid) $aid = $_POST['album_id'];


//echo $aid;



if ($_POST['sent'] == "sent") /////  form sent, save to db
{
	$update_string = "UPDATE ml_albums SET ";	
	$insert_string1 = "INSERT INTO ml_albums ("; $insert_string2 = "VALUES (";	
	foreach ($_POST as $key => $val)
	{
		if ($key != "sent" && $key != "submit" && $key != "album_id" ) {
			$update_string .= "`".$key.'` = "'.$val.'", '; 
			$insert_string1 .= $key.",";
			$insert_string2 .= '"'.$val.'", ';
		}
	}
	$update_string = rtrim ( $update_string , ", ");
	$update_string .= ' WHERE album_id = "' . $aid. '"'; 
	$insert_string1 = rtrim ( $insert_string1 , ", ");
	$insert_string = $insert_string1 . ") " . rtrim ( $insert_string2 , ", ") . ")";
	
	//echo $update_string . "<br>" . $insert_string;
	
	if ($aid == "xx") // do insert 
	{ 
	$result = mysql_query($insert_string); if ($result)$msg="<div class='goodMSG'>Album was added.</div><br>"; $aid = mysql_insert_id();
	} else {  // do update
	$result = mysql_query($update_string); if ($result)$msg="<div class='goodMSG'>Your changes were saved.</div><br>";
	}
}



if ($_POST['testid']) { $aid = $_POST['testid']; } // coming here from autosuggest on index.php

//require "functions.php";

if (!$aid) /// no id sent: user got here by manually entered artist name
{
	$dataa = $_POST['dataa'];
	header ("location: index.php?dataa=". $dataa . "&filter_field=artist&sort=artist");
} else {
	$query='SELECT * FROM ml_albums WHERE album_id = "'. $aid .'" ORDER BY creation_date DESC LIMIT 200';
}

$result= mysql_query($query);
$row= mysql_fetch_array($result, MYSQL_BOTH);


$section_id = $row['section_id'];
$artist = $row['artist'];
$freetext = $row['freetext'];
$explicit_lyrics_p = $row['explicit_lyrics_p'];
$title = $row['title'];
$year1 = $row['year1'];
$year2 = $row['year2'];
$cancon_p = $row['cancon_p'];
$creation_date = $row['creation_date'];
$creation_user = $row['creation_user'];
$creation_user_id = $row['creation_user_id'];
$playbox_p = $row['playbox_p'];
$playbox_date = $row['playbox_date'];
$cancon_p = $row['cancon_p'];
$medium_id = $row['medium_id'];
$keywords = $row['keywords'];


if ($_POST['delete']) /////  delete record
{
	$query = 'DELETE from ml_albums WHERE album_id = "'.$aid.'"';
	$result = mysql_query($query);
	//echo "q-$query  r-$result";
	if ($result) $msg = "<div class='goodMSG'>Record Deleted.</div><br>"; $del = 1;
}

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title><?php echo "KCR Music Database - ". $page_title; ?></title>
<link href="css/style.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="calendar_picker.js"></script>
<script type="text/javascript">
function validateDelete() {
   	//alert('debug 2.');
	if (confirm("Are you sure you want to delete this album? This cannot be undone.")) {
     		   return true 
			   } else {
			   return false 
			   }
}

//function validateD=() {
//if (document.form1.password.value == "") {
//					alert("The password cannot be blank."); 
//     		   		return false; 
//		   } else {
//			   		return true;
//}

</script>
</head>

<body>
<script type="text/javascript" src="wz_tooltip/wz_tooltip.js"></script>
<p><strong><font face="Helvetica Neue, Arial, Helvetica"><a href="index.php?filter_field=<?php if ($filter != "creation_date") echo $filter; ?>" class="factTextHead"><u>&lt;Music Database</u></a></font></strong> - <?php echo $page_title.$msg; if ($del) exit;?>
</p>

<?php
///////////  EDIT LAYOUT //////////////////////////////////////////////////////
if ($_SESSION['memberID']) { $showRI = "block";} else { $showRI = "none";}
		  
echo " <div id=\"edit\" style=\"display: $showRI; margin-left:20px\">";
?>


<table width="79%" border="1" cellspacing="0" cellpadding="8">
  <tr>
    <td width="49%" class="tdpers">Album Name
        <form action="edit_album.php" method="post">
        <input type="hidden" name="sent" value="sent" />
        <input name="title" type="text" id="title" class='textareaBox' value = "<?php echo $title;?>" size="45" />
</td>
    <td width="48%" class="tdpers">Artist
      <input name="artist" type="text" id="artist" class='textareaBox'  value = "<?php echo $artist;?>"/>
</td>
    <td width="3%" class="tdpers">&nbsp;</td>
  </tr>
  <tr>
    <td rowspan="2" class="tdpers"><table width="100%" border="0" cellspacing="8" cellpadding="0">
        <tr>
          <td>Year: </td>
          <td>Year2</td>
          <td>ISBN </td>
        </tr>
        <tr>
          <td><input name="year1" type="text" id="year1" value = "<?php echo $year1;?>" size="4" maxlength="4"/></td>
          <td><input name="year2" type="text" id="year2" value = "<?php echo $year2;?>" size="4" maxlength="4"/></td>
          <td><input name="isbn" type="text" id="isbn" size="25" value = "<?php echo $isbn;?>"/></td>
        </tr>
      </table>
      <table width="80%" cellpadding="8" cellspacing="0">
        <tr>
          <td ><div class="tdUnderlineS"><b>Date Added:</b><br /><input name="creation_date" type="text" id="creation_date" size="10" value = "<?php if ($creation_date) { echo substr($creation_date,0,10);} else { echo date("Y-m-d");} //$playbox_date; // ?>" onclick="displayDatePicker('creation_date');"/></div></td>
          <td  ><div class="tdUnderlineS"><b>in Playbox?</b><br /><input type='radio' name='playbox_p' value='P' id='playbox' <?php if ($playbox_p=="P" || !$playbox_p) echo "checked='checked'"; ?>/>Yes<br /><input type='radio' name='playbox_p' value='L' id='playbox' <?php if ($playbox_p=="L") echo "checked='checked'"; ?>/>No</div></td>
        </tr>
        <tr>
          <td ><div class="tdUnderlineS"><b>CanCon</b><br />
              <?php 
		  if ($cancon_p == "Y" || !$cancon_p) { echo " <label>
            <input type='radio' name='cancon_p' value='Y' id='cancon_p_0' checked='checked'/>Yes</label><br />
          <label><input type='radio' name='cancon_p' value='N' id='cancon_p_1' />No</label>";
		  } else {
		  echo " <label>
            <input type='radio' name='cancon_p' value='Y' id='cancon_p_0' />Yes</label><br />
          <label><input type='radio' name='cancon_p' value='N' id='cancon_p_1' checked='checked'/>No</label>";
		  }?>
          </p></div></td>
          <td ><div class="tdUnderlineS"><b>Explicit Lyrics?</b><br /><?php 
		  if ($explicit_lyrics_p == "Y") { echo " <label>
            <input type='radio' name='explicit_lyrics_p' value='Y' id='explicit_lyrics_p_0' checked='checked'/>Yes</label><br />
          <label><input type='radio' name='explicit_lyrics_p' value='N' id='explicit_lyrics_p_p_1' />No</label>";
		  } else {
		  echo " <label>
            <input type='radio' name='explicit_lyrics_p' value='Y' id='explicit_lyrics_p_0' />Yes</label><br />
          <label><input type='radio' name='explicit_lyrics_p' value='N' id='explicit_lyrics_p_p_1' checked='checked'/>No</label>";
		  }?></div></td>
        </tr>
      </table>
      <p><label>Medium 
        <select name="medium_id" id="select">
      <?php 
	  
$queryM='SELECT * FROM ml_media ORDER BY medium_label';
$resultM= mysql_query($queryM);
while ($rowM= mysql_fetch_array($resultM, MYSQL_NUM))
{
	$media_id_db = $rowM[0];
	$media_title_db = strtoupper ($rowM[1]);
	if ($medium_id == $media_id_db) { $item_selected = "selected='selected' "; } else { $item_selected = ""; }
	echo "<option $item_selected value ='$media_id_db'>$media_title_db</option>";
} 
	  ?>
        </select>
        <br />
        </label>&nbsp;</p>
    <p>Review/Notes<br />
      <textarea name="freetext" id="freetext" cols="50" rows="15"><?php echo $freetext;?></textarea>
      <br />
      <br />
      Added by:
      <?php 
	  if ($aid == "xx")  // new album, user logged in
	  { 
	  echo "<input name='creation_user' type='text' id='creation_user' size='45' value = '" . $_SESSION['screenName']."'/> 
	  <input name='creation_user_id' type='hidden' value='".$_SESSION['memberID']."'>"; 
	  } else {
	  echo "<input name='creation_user' type='text' id='creation_user' size='45' value = '" .$creation_user."'/> 
	  <input name='creation_user_id' type='hidden' value='".$creation_user_id."'>";
	  }
	  ?>
      <input name="album_id" type="hidden" value="<?php echo $aid; ?>" />
    </p>
    <p>
      <input type="submit" name="submit" id="submit" value="Save Changes" />
      
</p></td>
    <td class="tdpers"><br />
    </td>
    <td class="tdpers">&nbsp;</td>
  </tr>
  <tr>
    <td class="tdpers">
    <table align="center"><tr><td class="tdUnderlineS">
      <label> <strong>Section</strong><br />
        <br />
        <?php 
			  
$queryD='SELECT * FROM ml_sections ORDER BY section_label';
$resultD= mysql_query($queryD);
while ($rowD= mysql_fetch_array($resultD, MYSQL_NUM))
{
	$section_id_db = $rowD[0];
	$section_label_db = $rowD[1];
	$section_image_tag_db = $rowD[2];
	if ($section_id_db == $section_id) { $item_selected = "checked='checked' "; $bold ="<b>"; $current_color=$section_image_tag_db;} else { $item_selected = ""; $bold ="";}
echo "<img src='$section_image_tag_db' height='11' width = '11'/><input type='radio' name='section_id' value='$section_id_db' id='section_id_0' $item_selected/>$bold
        $section_label_db</label></b>
      <br />";	
}	
	?>
      </td></tr></table>
      <br />
      <br />
      <br />
      <br />
      <br />
      <br />
      <div align="right">Keywords <br />
        <input name="keywords" type="text" id="artist2" size="40" value = "<?php echo $keywords;?>"/>
        <br />
        <font size="2">(Separate keywords by commas)&nbsp;</font></div>
        </form>
      </td>
    <td class="tdpers"><form action="edit_album.php?aid=<?php echo $aid;?>" method="post" onSubmit="return validateDelete();" name="delete">&nbsp;<input name="delete" type="hidden" value="1" /><input name="Delete this Album" type="submit" value="Delete this Album" /></form></td>
  </tr>
</table>


</div>

<?php
///////////  VIEW LAYOUT //////////////////////////////////////////////////////
if ($_SESSION['memberID']) { $showRI = "none";} else { $showRI = "block";}
		  
echo " <div id=\"view\" style=\"display: $showRI; margin-left:20px\">";
?>
<table width="842" height="278">
  <tr>
    <td colspan="3" valign="top" class="tdpers"><table width="100%" height="486" border="0" cellpadding="3" cellspacing="0">
        <tr>
          <td height="28" colspan="5" valign="bottom"><font color="#666666">Album Name</font></td>
          <td width="3%" height="28" valign="bottom">&nbsp;</td>
          <td width="38%" height="28" valign="bottom"><font color="#666666">Artist</font></td>
          <td width="7%" height="28" valign="bottom">&nbsp;</td>
        </tr>
        <tr >
          <td onmouseover="Tip('Category: <?php echo $section_label_db; ?>')" onmouseout="UnTip()" height="44" colspan="5" background='<?php echo $current_color; ?>'><span class="factTextHead" ><font color='white'>&nbsp;<?php echo ucwords($title); ?></font></span></td>
          <td>&nbsp;</td>
          <td colspan="2" bgcolor="#333333"><span class="factTextHead"><font color='white'>&nbsp;<?php echo ucwords($artist); ?></font></span></td>
        </tr>
        <tr>
          <td width="13%" height="28" valign="bottom"><font color="#666666">Year</font></td>
          <td width="3%" valign="bottom">&nbsp;</td>
          <td colspan="3" valign="bottom"><font color="#666666">ISBN</font></td>
          <td valign="bottom">&nbsp;</td>
          <td valign="bottom"><font color="#666666">Date Added</font></td>
          <td valign="bottom">&nbsp;</td>
        </tr>
        <tr>
          <td bgcolor="#FFFFFF"><?php echo $year1;?></td>
          <td>&nbsp;</td>
          <td colspan="3" bgcolor="#FFFFFF"><?php echo $isbn;?></td>
          <td>&nbsp;</td>
          <td bgcolor="#FFFFFF"><?php echo substr($creation_date,0,10);?></td>
          <td>&nbsp;</td>
        </tr>
        <tr>
          <td height="29" valign="bottom"><font color="#666666">CanCon</font></td>
          <td height="29" valign="bottom">&nbsp;</td>
          <td width="20%" height="29" valign="bottom"><font color="#666666">In Playbox?</font></td>
          <td width="2%" valign="bottom">&nbsp;</td>
          <td width="14%" valign="bottom"><font color="#666666">Explicit Lyrics?</font></td>
          <td height="29" valign="bottom">&nbsp;</td>
          <td height="29" valign="bottom"><font color="#666666">Medium</font></td>
          <td height="29" valign="bottom">&nbsp;</td>
        </tr>
        <tr>
          <td bgcolor="#FFFFFF"><?php 
		  if ($cancon_p == "Y") { echo "<img src='images/National-Canadian-Flag.gif'> Yes";
		  } else {
		  echo "No";
		  }?></td>
          <td>&nbsp;</td>
          <td bgcolor="#FFFFFF"><?php 
		  if ($playbox_date) { echo "Since ". substr($playbox_date, 0, 10); } else { echo "nope"; } ?>          </td>
          <td>&nbsp;</td>
          <td bgcolor="#FFFFFF"><?php 
		  if ($explicit_lyrics_p == "Y") { echo "Yes"; } else { echo "No"; }?>
          </td>
          <td>&nbsp;</td>
          <td bgcolor="#FFFFFF">     <?php 	  
$queryM='SELECT * FROM ml_media WHERE medium_id = "' . $medium_id . '"';
$resultM= mysql_query($queryM);
$rowM= mysql_fetch_array($resultM, MYSQL_NUM);
	$media_id_db = $rowM[0];
	$media_title_db = $rowM[1];
	echo "$media_title_db";
	  ?></td>
          <td>&nbsp;</td>
        </tr>
        <tr>
          <td height="29" colspan="5" valign="bottom"><font color="#666666">Review/Notes</font></td>
          <td height="29" valign="bottom">&nbsp;</td>
          <td height="29" valign="bottom"><font color="#666666">Added By</font></td>
          <td height="29" valign="bottom">&nbsp;</td>
        </tr>
        <tr>
          <td height="280" colspan="5" valign="top" bgcolor="#FFFFFF"><?php echo $freetext;?></td>
          <td>&nbsp;</td>
          <td valign="top"><table width="100%" border="0" cellpadding="3" cellspacing="0">
            <tr>
              <td bgcolor="#FFFFFF"><?php echo $creation_user;?>&nbsp;</td>
            </tr>
            <tr>
              <td height="29" valign="bottom"><font color="#666666">Keywords</font></td>
            </tr>
            <tr>
              <td bgcolor="#FFFFFF"><?php echo $keywords;?>&nbsp;</td>
            </tr>
          </table></td>
          <td>&nbsp;</td>
        </tr>
      </table></td>
  </tr>
</table>
</div>
<p><strong><font face="Helvetica Neue, Arial, Helvetica"></font></strong></p>
</body>
</html>
